package com.renjiyue.dao;

import com.renjiyue.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class test1 {
    @Test
    public void selectUser() throws IOException{
        SqlSessionFactory sqlSessionFactory = null;
        //NyBatis的配置文件
        String resource = "mybatis-cmfig.xml";
        //使用类加载器加载mybatis的配置文件（它也加载关联的映射文件）
        try {
            InputStream is = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<User> users = mapper.selectUser();
        System.out.println(users);
        sqlSession.close();
    }
    @Test
    public void test2(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
        for (User user : userDao.selectUser()) {
            System.out.println(user);
        }

    }
    @Test
    public void test3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserDao userDao1 = (UserDao)context.getBean("userDao");
        for (User user : userDao1.selectUser()) {
            System.out.println(user);
        }
    }
}
